package com.cyh.pangu.ds;

/**
 * 数组的中间插入和扩容
 */
public class ArrayDemo {

    /** 数组 */
    private int[] arrsInt;

    /** 实际大小 */
    private int size;

    public ArrayDemo(int capacity) {
        this.arrsInt = new int[capacity];
        this.size = 0;
    }


    public void insert(int index, int value) {
        if (index < 0 || index > size || size > arrsInt.length-1) {
            throw new IndexOutOfBoundsException("下标越界");
        }
        for (int i =  size; i > index; i--) {
            arrsInt[i] = arrsInt[i-1];
        }
        arrsInt[index] = value;
        size++;
    }
    public void printArr() {
        System.out.println("size:" + size);
        for (int i =  0; i < arrsInt.length; i++) {
            System.out.println((i ) + ":" + arrsInt[i]);
        }
    }

    public static void main(String[] args) {
        ArrayDemo arrayDemo = new ArrayDemo(5);
        arrayDemo.insert(0, 10);
        arrayDemo.insert(1, 1);
        arrayDemo.insert(2,2);
        arrayDemo.insert(3, 3);
        arrayDemo.insert(1, 11);

        arrayDemo.printArr();
    }
}
